// pages/mine/mine.js
Page({
  /**
   * 页面的初始数据
   */
  data: {
    userInfo: '',
  },

  toLogin() {
    var that = this
    if (that.data.userInfo == '') {
      wx.navigateTo({
        url: '../login/login',
      })
    } else {
      wx.navigateTo({
        url: '../userDetail/userDetail',
      })
    }
  },

  shuchu(e) {
    var that = this
    if (that.data.userInfo == '') {
      wx.navigateTo({
          url: '../login/login',
        }),
        wx.showToast({
          title: '请先登录',
          icon: 'error'
        })
    } else {
      if (e.currentTarget.dataset.item == '电影票') {
        wx.navigateTo({
          url: '../orders/orders',
        })
      } else if (e.currentTarget.dataset.item == '优惠券') {
        wx.navigateTo({
          url: '../myCoupon/myCoupon',
        })
      } else if (e.currentTarget.dataset.item == '想看的电影') {
        wx.navigateTo({
          url: '../wantWatch/wantWatch',
        })
      } else if (e.currentTarget.dataset.item == '看过的电影') {
        wx.navigateTo({
          url: '../hadBeenMovie/hadBeenMovie',
        })
      } else if (e.currentTarget.dataset.item == '我的钱包') {
        wx.navigateTo({
          url: '../balance/balance',
        })
      } else {
        wx.clearStorageSync('username')
        wx.clearStorageSync('token')
        wx.showToast({
          title: '退出成功',
        })
        that.setData({
          userInfo: ''
        })
      }
    }
  },
  /**
   * 生命周期函数--监听页面加载
   */
  onLoad(options) {
    var that = this
    if (wx.getStorageSync('username') != '') {
      wx.request({
        url: 'http://localhost:8081/api/userInfo/getUserInfoByUsername',
        data: {
          username: wx.getStorageSync('username')
        },
        header: {
          'token': wx.getStorageSync('token')
        },
        method: "GET",
        success: function (res) {
          if (res.data.code == 10000) {
            wx.clearStorageSync('userInfo'),
            wx.clearStorageSync('username'),
              wx.clearStorageSync('token')
            that.setData({
                userInfo: '',
              }),
              wx.showToast({
                title: '信息已过期，请重新登录',
                icon: 'error'
              })
          } else {
            wx.setStorageSync('userInfo', res.data.result)
            that.setData({
              userInfo: res.data.result
            })
          }
        }
      })
    }
  },

  /**
   * 生命周期函数--监听页面初次渲染完成
   */
  onReady() {

  },

  /**
   * 生命周期函数--监听页面显示
   */
  onShow() {
    var that = this
    if (wx.getStorageSync('username') != '') {
      wx.request({
        url: 'http://localhost:8081/api/userInfo/getUserInfoByUsername',
        data: {
          username: wx.getStorageSync('username')
        },
        header: {
          'token': wx.getStorageSync('token')
        },
        method: "GET",
        success: function (res) {
          if (res.data.code == 10008) {
            wx.setStorageSync('userInfo', res.data.result)
            that.setData({
              userInfo: res.data.result
            })
            console.log(that.data.userInfo)
          }else{
            wx.clearStorageSync("userInfo")
            wx.clearStorageSync("username")
            that.setData({
              userInfo : ''
            })
          }
        }
      })
    }
  },

  /**
   * 生命周期函数--监听页面隐藏
   */
  onHide() {

  },

  /**
   * 生命周期函数--监听页面卸载
   */
  onUnload() {

  },

  /**
   * 页面相关事件处理函数--监听用户下拉动作
   */
  onPullDownRefresh() {

  },

  /**
   * 页面上拉触底事件的处理函数
   */
  onReachBottom() {

  },

  /**
   * 用户点击右上角分享
   */
  onShareAppMessage() {

  }
})